package ext.generic.wfaugment;

import java.util.ArrayList;
import java.util.Hashtable;

import wt.fc.Persistable;
import wt.fc.ReferenceFactory;
import wt.fc.WTReference;

import wt.session.SessionServerHelper;

import wt.workflow.definer.WfAssignedActivityTemplate;
import wt.workflow.engine.WfActivity;
import wt.workflow.engine.WfProcess;
import wt.workflow.work.WorkItem;

import com.ptc.core.ui.validation.DefaultSimpleValidationFilter;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationStatus;
/**
 *搜索用户来安装流程参与者的action入口校验程序
 * @author Harry Cao
 */
public class WorkflowAddParticipantsBySearchValidationFilter extends DefaultSimpleValidationFilter
{

    public WorkflowAddParticipantsBySearchValidationFilter()
    {
    }
    /**
     *入口校验方法
     * @param uivalidationkey
     * @param uivalidationcriteria
     * @return
     */
    public UIValidationStatus preValidateAction(UIValidationKey uivalidationkey, UIValidationCriteria uivalidationcriteria)
    {
        UIValidationStatus uivalidationstatus = UIValidationStatus.ENABLED;
        boolean flag = SessionServerHelper.manager.setAccessEnforced(false);
        try
        {
            WTReference wtreference = uivalidationcriteria.getContextObject();
            String[] aciveoid = (String[]) uivalidationcriteria.getParameterMap().get("activeoid");
            if (wtreference == null || aciveoid == null)
                return uivalidationstatus;
            String aciveoidfinal = aciveoid[0];
            if (aciveoidfinal == null)
                return uivalidationstatus;
            Persistable persistable = wtreference.getObject();
            if (persistable instanceof WorkItem)
            {
                WorkItem workItem = (WorkItem) persistable;
                Persistable persistable2 = workItem.getPrimaryBusinessObject().getObject();
                WfActivity wfactivity = (WfActivity) workItem.getSource().getObject();
                WfProcess wfprocess = wfactivity.getParentProcess();
                Hashtable activehtInguide = WorkFlowStepUtil.getWorkFlowStepInReviewGuideByName(persistable2, wfprocess);
                ReferenceFactory referencefactory = new ReferenceFactory();
                WfAssignedActivityTemplate wfatp = (WfAssignedActivityTemplate) referencefactory.getReference(aciveoidfinal).getObject();
                if (wfatp != null)
                {
                    String rolestr = (String) activehtInguide.get(wfatp.getName());
                    ArrayList arraylist = WorkFlowStepUtil.getUserInTheContainerTeam(persistable2, rolestr);
                    if (arraylist != null && arraylist.size() > 0)
                        uivalidationstatus = UIValidationStatus.HIDDEN;
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            SessionServerHelper.manager.setAccessEnforced(flag);
        }
        return uivalidationstatus;
    }
}
